//running stacked regression of enrollment on minimum wage changes

global main "[YOUR PATH HERE]"
global output "${main}/output"

//switch to rerun the build stacked data do file
local switch_rerun_stacking = 1

local switch_controls = 1 //adds controls for free college (dummies) and for the unemployment rate

local outcomes youtht_e adult_e disloc_e


********************************************************************************
**CREATE STACKED DATA**
********************************************************************************

if `switch_rerun_stacking'{
	do "${main}/code/build_stacked_data.do"
}

********************************************************************************
**ENROLLMENT DATA**
********************************************************************************
use "${main}/data/clean_wia.dta", clear
		drop _merge

	//add control vars	
	if `switch_controls'{
		//generate indicators for if a state has a free CC program in a given year
		gen free_cc = 0
			replace free_cc = 1 if statefips == 5  & year > 2016
			replace free_cc = 1 if statefips == 6  & year > 2018
			replace free_cc = 1 if statefips == 9  & year > 2019
			replace free_cc = 1 if statefips == 10 & year > 2005
			replace free_cc = 1 if statefips == 13 & year > 2013
			replace free_cc = 1 if statefips == 15 & year > 2017
			replace free_cc = 1 if statefips == 18 & year > 1990
			replace free_cc = 1 if statefips == 19 & year > 2019
			replace free_cc = 1 if statefips == 20 & year > 2016
			replace free_cc = 1 if statefips == 21 & year > 2016
			replace free_cc = 1 if statefips == 22 & year > 2016
			replace free_cc = 1 if statefips == 24 & year > 2016
			replace free_cc = 1 if statefips == 28 & year > 2016
			replace free_cc = 1 if statefips == 29 & year > 2016
			replace free_cc = 1 if statefips == 30 & year > 2016
			replace free_cc = 1 if statefips == 32 & year > 2016
			replace free_cc = 1 if statefips == 34 & year > 2016
			replace free_cc = 1 if statefips == 36 & year > 2016
			replace free_cc = 1 if statefips == 40 & year > 2016
			replace free_cc = 1 if statefips == 41 & year > 2016
			replace free_cc = 1 if statefips == 44 & year > 2016
			replace free_cc = 1 if statefips == 46 & year > 2016
			replace free_cc = 1 if statefips == 47 & year > 2016
			replace free_cc = 1 if statefips == 53 & year > 2016
			replace free_cc = 1 if statefips == 54 & year > 2016
			
		//add controls for unemployment rate by state
		preserve
			use "${main}/data/clean_controls_labormarket.dta", clear
			
			keep statefips unemp_rate year
			
			tempfile unemployment
			save `unemployment'
		restore 
		
		merge m:1 statefips year using `unemployment', keep(match) nogen 
			//need to assert match master so when we restrict years we can get rid of those not in our sample
			
		//add controls for log state population (18-30 years old)
		rename year year_academic
		merge m:1 statefips year_academic using "${main}/data/census_yearly_statepop.dta", keep(match) nogen 
		
	}
	else{
		rename year year_academic
	}
	local ytitle "pell `var'"
	
	foreach x of local outcomes{
		gen ln_`x' = ln(`x')
	} 
	
	merge 1:m year_academic statefips using "${main}/data/mw_stacked.dta", keep(match) //drop years outside of enrollment panel

	********************************************************************************
	**GRAPHING**
	********************************************************************************

	matrix col1 = J(8,2,.)
	matrix col2 = J(8,2,.)
	matrix col3 = J(8,3,.)
	matrix results = J(8,7,.)

	* define treatment vector
	global treat F*treat L*treat

	*linear combinations of post-reg coeffs
	global lincomm3 F3treat - F1treat
	global lincomm2 F2treat - F1treat
	global lincomm1 F1treat - F1treat
	global lincom0 L0treat - F1treat
	global lincom1 L1treat - F1treat
	global lincom2 L2treat - F1treat
	global lincom3 L3treat - F1treat
	global lincom4 L4treat - F1treat


	preserve
		eststo youth:  reghdfe ln_youtht_e $treat  , ///
				a(i.event#i.statefips i.event#i.rel_time i.event#i.control_origeventpost ///
				i.event#control_fedeventpost i.event#control_othereventpost) cluster(dmw_statefips)
					local dof = e(df_r)
					local i = 1
					foreach year in 3 2 1{
						lincom (${lincomm`year'})
							matrix col1[`i',1] = r(estimate)
							matrix col1[`i++',2] = r(se)
					}
					foreach year in 0 1 2 3 4{
						lincom (${lincom`year'})
							matrix col1[`i',1] = r(estimate)
							matrix col1[`i++',2] = r(se)
					}

		eststo adult:  reghdfe ln_adult_e $treat   , ///
				a(i.event#i.statefips i.event#i.rel_time i.event#i.control_origeventpost ///
				i.event#control_fedeventpost i.event#control_othereventpost) cluster(dmw_statefips) 
				local i = 1
				foreach year in 3 2 1{
					lincom (${lincomm`year'})
						matrix col2[`i',1] = r(estimate)
						matrix col2[`i++',2] = r(se)
				}
				foreach year in 0 1 2 3 4{
					lincom (${lincom`year'})
						matrix col2[`i',1] = r(estimate)
						matrix col2[`i++',2] = r(se)
				}
	eststo disloc:  reghdfe ln_disloc_e $treat   , ///
			a(i.event#i.statefips i.event#i.rel_time i.event#i.control_origeventpost ///
			i.event#control_fedeventpost i.event#control_othereventpost) cluster(dmw_statefips) 
				local i = 1
				foreach year in 3 2 1{
					lincom (${lincomm`year'})
						matrix col3[`i',1] = r(estimate)
						matrix col3[`i++',2] = r(se)
				}
				foreach year in 0 1 2 3 4{
					lincom (${lincom`year'})
						matrix col3[`i',1] = r(estimate)
						matrix col3[`i++',2] = r(se)
				}
					
		matrix col3[1,3] = -3
		matrix col3[2,3] = -2
		matrix col3[3,3] = -1
		matrix col3[4,3] = 0
		matrix col3[5,3] = 1
		matrix col3[6,3] = 2
		matrix col3[7,3] = 3
		matrix col3[8,3] = 4

	matrix results = col1, col2, col3

	clear
	matlist results
	svmat results

	rename results1  coeff_y
	rename results2  se_y
	rename results3  coeff_a
	rename results4  se_a
	rename results5  coeff_d
	rename results6  se_d
	rename results7  time

	foreach x in y a d{
		gen ci_upper_`x' = coeff_`x' + invttail(`dof', 0.025)*se_`x'
		gen ci_lower_`x' = coeff_`x' - invttail(`dof', 0.025)*se_`x'
	}

	//graphing style things
	grstyle init
	grstyle set plain
	grstyle set size 8pt: subheading axis_title
	 
	twoway rarea ci_lower_a ci_upper_a  time, color(eltgreen%45)   ||  ///
			line coeff_a time, lcolor(eltgreen) mcolor(eltgreen)   || ///
			rarea ci_lower_d ci_upper_d  time, color(sand%45)   ||  ///
			line coeff_d time, lcolor(sand) mcolor(sand)     || ///
			rarea ci_lower_y ci_upper_y  time, color(sienna%40)    ||  ///
			line coeff_y time, lcolor(sienna) mcolor(sienna)          ///
			ylab(, grid labsize(small)) yscale(range()) yline(0, lp(dash) lc(gs10)) ///
			xline(-1, lp(dash) lc(black)) xlab(-3(1)4, grid labsize(small)) legend(col(3) ///
			order(2 "Adults" 4 "Disloc" 6 "Youth") size(small)) ytitle("") ///
			xtitle("years since minimum wage change") legend(region(lstyle(none)))
		graph export "${output}/MWwia_${dmwlevel}_logs.pdf", replace
	
	//export results to tables raw
	export excel ${output}/tables_raw.xlsx, sheet(wia_log_results_${dmwlevel}) sheetreplace firstrow(var)
			
restore

//version with controls
if `switch_controls'{
	preserve
		eststo youth:  reghdfe ln_youtht_e $treat free_cc unemp_rate lnpop_1830 , ///
				a(i.event#i.statefips i.event#i.rel_time i.event#i.control_origeventpost ///
				i.event#control_fedeventpost i.event#control_othereventpost) cluster(dmw_statefips)	
					local dof = e(df_r)
					local i = 1
					foreach year in 3 2 1{
						lincom (${lincomm`year'})
							matrix col1[`i',1] = r(estimate)
							matrix col1[`i++',2] = r(se)
					}
					foreach year in 0 1 2 3 4{
						lincom (${lincom`year'})
							matrix col1[`i',1] = r(estimate)
							matrix col1[`i++',2] = r(se)
					}

		eststo adult:  reghdfe ln_adult_e $treat free_cc unemp_rate lnpop_1830 , ///
				a(i.event#i.statefips i.event#i.rel_time i.event#i.control_origeventpost ///
				i.event#control_fedeventpost i.event#control_othereventpost) cluster(dmw_statefips) 
					local i = 1
					foreach year in 3 2 1{
						lincom (${lincomm`year'})
							matrix col2[`i',1] = r(estimate)
							matrix col2[`i++',2] = r(se)
					}
					foreach year in 0 1 2 3 4{
						lincom (${lincom`year'})
							matrix col2[`i',1] = r(estimate)
							matrix col2[`i++',2] = r(se)
					}
		eststo disloc:  reghdfe ln_disloc_e $treat free_cc unemp_rate lnpop_1830 , ///
				a(i.event#i.statefips i.event#i.rel_time i.event#i.control_origeventpost ///
				i.event#control_fedeventpost i.event#control_othereventpost) cluster(dmw_statefips) 
					local i = 1
					foreach year in 3 2 1{
						lincom (${lincomm`year'})
							matrix col3[`i',1] = r(estimate)
							matrix col3[`i++',2] = r(se)
					}
					foreach year in 0 1 2 3 4{
						lincom (${lincom`year'})
							matrix col3[`i',1] = r(estimate)
							matrix col3[`i++',2] = r(se)
					}
					matrix col3[1,3] = -3
					matrix col3[2,3] = -2
					matrix col3[3,3] = -1
					matrix col3[4,3] = 0
					matrix col3[5,3] = 1
					matrix col3[6,3] = 2
					matrix col3[7,3] = 3
					matrix col3[8,3] = 4

		matrix results = col1, col2, col3

		clear
		matlist results
		svmat results

		rename results1  coeff_y
		rename results2  se_y
		rename results3  coeff_a
		rename results4  se_a
		rename results5  coeff_d
		rename results6  se_d
		rename results7  time

		foreach x in y a d{
			gen ci_upper_`x' = coeff_`x' + invttail(`dof', 0.025)*se_`x'
			gen ci_lower_`x' = coeff_`x' - invttail(`dof', 0.025)*se_`x'
		}


		//graphing style things
		grstyle init
		grstyle set plain
		grstyle set size 8pt: subheading axis_title

		twoway  rarea ci_lower_a ci_upper_a  time, color(eltgreen%45) ||  ///
			line coeff_a time, lcolor(eltgreen) mcolor(eltgreen)  || ///
			rarea ci_lower_d ci_upper_d  time, color(sand%45)  ||  ///
			line coeff_d time, lcolor(sand) mcolor(sand)    || ///
			rarea ci_lower_y ci_upper_y  time, color(sienna%40)   ||  ///
			line coeff_y time, lcolor(sienna) mcolor(sienna)        ///
			ylab(, grid labsize(small)) yscale(range()) yline(0, lp(dash) lc(gs10)) ///
			xline(-1, lp(dash) lc(black)) xlab(-3(1)4, grid labsize(small)) legend(col(3) ///
			order(2 "Adults" 4 "Disloc" 6 "Youth") size(small)) ytitle("") ///
			xtitle("years since minimum wage change") legend(region(lstyle(none))) ///
			note(`"controls for log 18-30 year-old population, free CC, and unemployment rates"')	
		graph export "${output}/MWwia_${dmwlevel}_controls_logs.pdf", replace
		
		export excel ${output}/tables_raw.xlsx, sheet(wia_log_results_cntls_${dmwlevel}) sheetreplace firstrow(var)
				
	restore
}
